﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;

namespace BookStore.admin
{
    public partial class HoaDon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CTHDmoi_DV_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            CTHD_GVDS.DataBind();
            CTHD_GV.DataBind();
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception == null && e.AffectedRows == 1)
            {
                // The label displays the primary key of the recently inserted row.
                using (var context = new BookStoreEntities())
                {
                    string makh = (string)e.Values[0];
                    int ma = Convert.ToInt32(makh);
                    string diachihd = (string)e.Values[1];
                    string diachigh = (string)e.Values[2];
                    string dt = (string)e.Values[3];
                    DateTime ng = Convert.ToDateTime(e.Values[4]);
                    int trth = Convert.ToInt32(e.Values[5]);
                    var k = (from n in context.HoaDons
                            where n.MaKH == ma &&
                                       n.DiaChiHoaDon == diachihd &&
                                       n.DiaChiGiaoHang == diachigh &&
                                       n.SoDT == dt &&
                                       n.NgayThang == ng &&
                                       n.TrangThai == trth
                                    select n).OrderByDescending(n => n.MaHD);
                    BookStore.HoaDon a = k.First();
                    Response.Redirect("./HoaDon.aspx?mahd=" + a.MaHD.ToString());
                }
            }
        }
    }
}